﻿using UnityEngine;

public class NoticeTipsWin : MonoBase
{
	private NoticeTipsController _controller;
	private bool _isRunning;

	private RectTransform _uiBg;
	private GameObject _textPrefab;

	protected override void Awake()
	{
		base.Awake();
		_textPrefab = transform.Find("TextPrefab").gameObject;
		_textPrefab.SetActive(false);
		_uiBg = transform.Find("Background").GetComponent<RectTransform>();
	}

	protected override void Start()
	{
		base.Start();
		UpdateInfo();
	}

	public void UpdateInfo()
	{
		if (_isRunning) return;
		AddItem();
	}

	private void AddItem()
	{
		if (_controller.TipsQueue.Count > 0)
		{
			_isRunning = true;
			string tips = _controller.TipsQueue.Dequeue();
			GameObject go = GameTools.AddChild(_uiBg, _textPrefab);
			go.SetActive(true);
			NoticeTipsItem item = go.AddComponent<NoticeTipsItem>();
			item.OnShow.AddListener(OnTipsShow);
			item.OnHide.AddListener(OnTipsHide);
			item.SetInfo(tips);
		}
	}

	private void OnTipsShow()
	{
		_isRunning = false;
		UpdateInfo();
	}

	private void OnTipsHide()
	{
		if (_uiBg.transform.childCount > 0) return;
		WindowManager.GetInstance().CloseWindow(Window.NoticeTipsWin);
	}

	public float BgWidth { get { return _uiBg.sizeDelta.x; } }

	public NoticeTipsController Serves { set { _controller = value; } }
}